﻿Imports System.Data.SqlClient

Public Class DeclaredVariable
    Private mSqlDbType As SqlDbType
    Private mFrameworkType As Type
    Private mDeclaredType As String
    Private mDefaultValue As Object
    Private mUIAttribute As UIAttribute
    Private mConnection As SqlConnection

    Public Property Name As String
    Public ReadOnly Property SqlDbType As SqlDbType
        Get
            If IsNothing(mSqlDbType) Then
                Return Data.SqlDbType.Variant
            Else
                Return mSqlDbType
            End If
        End Get
    End Property

    Public ReadOnly Property FrameworkType As Type
        Get
            If IsNothing(mFrameworkType) Then
                Return GetType(Object)
            Else
                Return mFrameworkType
            End If
        End Get
    End Property

    Public Property DeclaredType As String
        Get
            Return mDeclaredType
        End Get

        Set(value As String)
            Select Case value.ToLower
                Case "bigint"
                    mFrameworkType = GetType(Long)
                    mSqlDbType = SqlDbType.BigInt
                Case "binary", "varbinary"
                    mFrameworkType = GetType(Byte())
                    mSqlDbType = SqlDbType.VarBinary
                Case "bit"
                    mFrameworkType = GetType(Boolean)
                    mSqlDbType = SqlDbType.Bit
                Case "char"
                    mFrameworkType = GetType(String)
                    mSqlDbType = SqlDbType.Char
                Case "date"
                    mFrameworkType = GetType(DateTime)
                    mSqlDbType = SqlDbType.Date
                Case "datetime"
                    mFrameworkType = GetType(DateTime)
                    mSqlDbType = SqlDbType.DateTime
                Case "datetime2"
                    mFrameworkType = GetType(DateTime)
                    mSqlDbType = SqlDbType.DateTime2
                Case "datetimeoffset"
                    mFrameworkType = GetType(DateTimeOffset)
                    mSqlDbType = SqlDbType.DateTimeOffset
                Case "decimal"
                    mFrameworkType = GetType(Decimal)
                    mSqlDbType = SqlDbType.Decimal
                Case "float"
                    mFrameworkType = GetType(Double)
                    mSqlDbType = SqlDbType.Float
                Case "image"
                    mFrameworkType = GetType(Byte())
                    mSqlDbType = SqlDbType.Binary
                Case "int", "integer"
                    mFrameworkType = GetType(Integer)
                    mSqlDbType = SqlDbType.Int
                Case "money"
                    mFrameworkType = GetType(Decimal)
                    mSqlDbType = SqlDbType.Money
                Case "nchar"
                    mFrameworkType = GetType(String)
                    mSqlDbType = SqlDbType.NChar
                Case "ntext"
                    mFrameworkType = GetType(String)
                    mSqlDbType = SqlDbType.NText
                Case "numeric"
                    mFrameworkType = GetType(Decimal)
                    mSqlDbType = SqlDbType.Decimal
                Case "nvarchar"
                    mFrameworkType = GetType(String)
                    mSqlDbType = SqlDbType.NVarChar
                Case "real"
                    mFrameworkType = GetType(Single)
                    mSqlDbType = SqlDbType.Real
                Case "smallint"
                    mFrameworkType = GetType(Int16)
                    mSqlDbType = SqlDbType.SmallInt
                Case "text"
                    mFrameworkType = GetType(String)
                    mSqlDbType = SqlDbType.Text
                Case "time"
                    mFrameworkType = GetType(TimeSpan)
                    mSqlDbType = SqlDbType.Time
                Case "tinyint"
                    mFrameworkType = GetType(Byte)
                    mSqlDbType = SqlDbType.TinyInt
                Case "varchar"
                    mFrameworkType = GetType(String)
                    mSqlDbType = SqlDbType.VarChar
                Case "variant"
                    mFrameworkType = Nothing
                    mSqlDbType = Nothing
            End Select
            mDeclaredType = value
        End Set
    End Property

    Public ReadOnly Property UIAttribute As UIAttribute
        Get
            Return mUIAttribute
        End Get
    End Property
    Public Property DefaultValue As Object
        Get
            Return mDefaultValue
        End Get
        Set(value As Object)
            mDefaultValue = value
        End Set
    End Property

    Public Sub New(ByVal Name As String, ByVal declaredType As String, ByVal comment As String)
        If Not String.IsNullOrEmpty(declaredType) Then
            Me.DeclaredType = declaredType
        End If

        If Not String.IsNullOrEmpty(comment) Then
            mUIAttribute = New UIAttribute(comment)
        End If
        Me.Name = Name
    End Sub

End Class
